package cn.bugstack.chatgpt.data.config;

import cn.bugstack.chatgpt.common.Constants;
import cn.bugstack.chatgpt.session.OpenAiSession;
import cn.bugstack.chatgpt.session.OpenAiSessionFactory;
import cn.bugstack.chatgpt.session.defaults.DefaultOpenAiSessionFactory;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author Fuzhengwei bugstack.cn @小傅哥
 * @description OpenAiSession 工厂配置开启
 * @create 2023-07-16 08:07
 */
@Configuration
//@EnableConfigurationProperties(ChatGPTSDKConfigProperties.class)
@Data
@RequestMapping(value = "/insert", method = RequestMethod.POST)
@ResponseBody

//@RequestParam(value = "apiHost", required = false) String apiHost,@RequestParam(value = "apiKey", required = false) String apiKey
public class ChatGPTSDKConfig {
    @Value("apiHost")
    String apiHost;
    @Value("apiKey")
    String apiKey;
    // @Resource
//
//    public OpenAiSession openAiSession(){
//        return null;
//    }
//    @Resource
    @Bean
    public String apiHost(){
        return apiHost;
    }
    @Bean
    public String  apiKey(){
        return  apiKey;
    }

    @Bean
    public OpenAiSession openAiSession(@RequestParam(value = "apiHost", required = false) String apiHost,@RequestParam(value = "apiKey", required = false) String apiKey){
        String ApiHost = Constants.NULL.equals("NULL") ? "https://pro-share-aws-api.zcyai/" : apiHost;
        String ApiKey = Constants.NULL.equals("NULL") ? "sk-IB4eZyIU4jzfT1OJ38392837Be044e4bBe33AcFdF045708f": apiKey;
        // 1. 配置文件
        cn.bugstack.chatgpt.session.Configuration configuration = new cn.bugstack.chatgpt.session.Configuration();
        configuration.setApiHost(ApiHost);
//        configuration.setAuthToken(properties.getAuthToken());
        configuration.setApiKey(ApiKey);

        // 2. 会话工厂
        OpenAiSessionFactory factory = new DefaultOpenAiSessionFactory(configuration);

        // 3. 开启会话
        return factory.openSession();
    }


//    @Bean
//    public OpenAiSession openAiSession(ChatGPTSDKConfigProperties properties) {
//        // 1. 配置文件
//        cn.bugstack.chatgpt.session.Configuration configuration = new cn.bugstack.chatgpt.session.Configuration();
//        configuration.setApiHost(properties.getApiHost());
//        configuration.setAuthToken(properties.getAuthToken());
//        configuration.setApiKey(properties.getApiKey());
//
//        // 2. 会话工厂
//        OpenAiSessionFactory factory = new DefaultOpenAiSessionFactory(configuration);
//
//        // 3. 开启会话
//        return factory.openSession();
//    }

}
